package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Account;
import com.zretc.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 何亮龙
 * 后端用户控制器
 */
@Controller
@RequestMapping("back")
public class BackAccountController {
    private static final Logger logger = LoggerFactory.getLogger(FrontAccountController.class);

    @Autowired
    private AccountService accountService;

    // 获取用户列表
    @GetMapping("account")
    @ResponseBody
    public PageInfo<Account> getAccountsList(String accountId,
                                             String accountName,
                                             String accountGender,
                                             String accountPwd,
                                             String accountPhone,
                                             String accountPicture,
                                             String accountRegTime,
                                             @RequestParam("page") String pageNum,
                                             @RequestParam("limit") String pageSize) {
        // 组装条件
        Map<String, String> condition = new HashMap<>();
        condition.put("accountId", accountId);
        condition.put("accountName", accountName);
        condition.put("accountGender", accountGender);
        condition.put("accountPwd", accountPwd);
        condition.put("accountPhone", accountPhone);
        condition.put("accountPicture", accountPicture);
        condition.put("accountRegTime", accountRegTime);
        condition.put("pageNum", pageNum);
        condition.put("pageSize", pageSize);
        // 调用业务层
        PageInfo<Account> pageInfo = accountService.getAccountByCondition(condition);
        // 返回json数据
        return pageInfo;
    }

    // 获取用户详情
    @GetMapping("account/{name}")
    @ResponseBody
    public Account getAccountByAccountName(@PathVariable("name") String accountName) {
        return accountService.getByAccountName(accountName);
    }
    // 获取用户详情
    @GetMapping("getAccountByAccountId")
    @ResponseBody
    public Account getAccountByAccountId(@PathVariable("accountId") String accountId) {
        Integer a = Integer.valueOf(accountId);
        return accountService.getByAccountId(a);
    }
    // 删除单条用户信息

    @DeleteMapping("/delectByAccountId/{accountId}")
    @ResponseBody boolean delectByAccountId(@PathVariable("accountId") String accountId){
        Integer a = Integer.valueOf(accountId);
        return accountService.deleteByAccountId(a);
    }
}